package org.helgoboss.single_source_builder_main

import java.io.{File, InputStream, PrintWriter}

class SingleSourceBuilder (
    val sourceCode: String,
    val language: Language,
    val targetProjectDir: Option[File] = None
) {    
    
    /**
     * Analyzes the source code and returns the project descriptor.
     */
    def analyzeSourceCode: ProjectDescriptor = {
        language.analyzeSourceCode(sourceCode)
    }
    
    /**
     * Creates the project file tree and returns its root dir.
     */
    def createProjectFileTree(projectDescriptor: ProjectDescriptor, pomContent: String, sourceCode: String): File = {
        import org.helgoboss.scala_file_tree_builder.FileTree
        import org.helgoboss.scala_rich_file.RichFile._
        
        val projectDir = determineTargetProjectDir(projectDescriptor)
        projectDir.makeSureDirExistsAndIsEmpty
        
        new FileTree {
            def structure {
                "pom.xml" << pomContent
                language.generateTargetSourceFileLocation(projectDescriptor) << sourceCode
            }
        }.createIn(projectDir)
        
        projectDir
    }
    
    /**
     * Generates the content of the pom file based on the given project descriptor.
     */
    def generatePomContent(projectDescriptor: ProjectDescriptor) = {
        import projectDescriptor._
        
        val exportedPackage = (artifact.groupId + "." + artifact.artifactId).replaceAll("-", "_")
    
        val pom = 
            <project xmlns="http://maven.apache.org/POM/4.0.0" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
                
            <modelVersion>4.0.0</modelVersion>
            
            {parentArtifactXml}
            
            {artifactXml}
            
            {dependenciesXml}
            
            <build>
                <plugins>
                    {language.generatePluginsXml(projectDescriptor)}
                    
                    <plugin>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>maven-bundle-plugin</artifactId>
                        <configuration>
                            <instructions>
                                <Export-Package>{exportedPackage}</Export-Package>
                                <Private-Package>{exportedPackage}.*</Private-Package>
                            </instructions>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </project>

    }
    
    
    /**
     * Runs Maven in the specified project dir with the specified arguments.
     */
    def buildWithMaven(projectDir: File, arguments: Array[String], writer: PrintWriter) = {
        // TODO
    }
    
    /**
     * Installs the bundle corresponding to the given project descriptor into this OSGi container or refreshes it.
     * This uses OBR repositories. So this only works if the correct repository is installed and the bundle has been installed
     * into this repository.
     */
    def installOrRefreshBundleFromObr(projectDescriptor: ProjectDescriptor) {
        // TODO
    }
    
    
    /**
     * Attempts to start the bundle corresponding to the given project descriptor as Commandlet.
     */
    def startAsCommandlet(projectDescriptor: ProjectDescriptor) {
        // TODO
    }
    
    private def determineTargetProjectDir(projectDescriptor: ProjectDescriptor) = {
        targetProjectDir.getOrElse {
            val pathToTempDir = System.getProperty("java.io.tmpdir")
            new File(pathToTempDir, projectDescriptor.artifact.mkString("_"))
        }
    }
    
}